using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace IceAge
{
    public class MyComboItem
    {
        private string _id = "";
        private string _value = "";

        public MyComboItem()
        {
        }

        public MyComboItem(string ID, string Value)
        {
            _id = ID;
            _value = Value;
        }

        public string ID
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }

        public string Value
        {
            get
            {
                return _value;
            }
            set
            {
                _value = value;
            }
        }
    }
    public class CommonLibrary
    {
        #region Bien toan cuc
        public static string USER_NAME = string.Empty;
        public static DateTime TRAN_DATE = DateTime.MinValue;
        public static string ROLE = string.Empty;
        public static string BRANCH_ID = string.Empty;
        public static DataSet STOCK_INFO = null;
        public static DataSet XML_DATA = null;
        public static string SYSTEM_STATUS = string.Empty;
        public static string TRADING_STATUS = string.Empty;
        public static DateTime SETTLE_DATE = DateTime.MaxValue;

        public static string FIRM_CODE = string.Empty;
        public static string COMPANY_NAME = string.Empty;
        public static string ADDRESS = string.Empty;
        public static string CONTACT = string.Empty;

        #endregion

        #region Cac ham truy cap webservice
        //public static DataSet ExecuteReader(string business, object[] parameters)
        //{
        //    CenturyFox.Service data_access = new CenturyFox.Service();
        //    CenturyFox.AuthHeader _orderHeader = new CenturyFox.AuthHeader();
        //    _orderHeader.Password = "nimd@";
        //    _orderHeader.Username = "@dmin";
        //    data_access.AuthHeaderValue = _orderHeader;
        //    data_access.Timeout = global::IceAge.Properties.Settings.Default.ClientTimeout;
        //    return data_access.ExecuteReader(business, parameters);
        //}
        //public static DataSet GetXmlData()
        //{
        //    CenturyFox.Service data_access = new CenturyFox.Service();
        //    CenturyFox.AuthHeader _orderHeader = new CenturyFox.AuthHeader();
        //    _orderHeader.Password = "nimd@";
        //    _orderHeader.Username = "@dmin";
        //    data_access.AuthHeaderValue = _orderHeader;
        //    data_access.Timeout = global::IceAge.Properties.Settings.Default.ClientTimeout;
        //    return data_access.GetXmlData();
        //}
        //public static void ExecuteNonquery(string business, object[] parameters)
        //{
        //    CenturyFox.Service data_access = new CenturyFox.Service();
        //    CenturyFox.AuthHeader _orderHeader = new CenturyFox.AuthHeader();
        //    _orderHeader.Password = "nimd@";
        //    _orderHeader.Username = "@dmin";
        //    data_access.AuthHeaderValue = _orderHeader;
        //    data_access.ExecuteNonquery(business, parameters);
        //}
        //public static object ExecuteScalar(string business, object[] parameters)
        //{
        //    CenturyFox.Service data_access = new CenturyFox.Service();
        //    CenturyFox.AuthHeader _orderHeader = new CenturyFox.AuthHeader();
        //    _orderHeader.Password = "nimd@";
        //    _orderHeader.Username = "@dmin";
        //    data_access.AuthHeaderValue = _orderHeader;
        //    return data_access.ExecuteScalar(business, parameters);
        //}

        //Code cho TuLM
        public static DataSet GetDataSetFromProcedure(string sql_procedure, string sql_parameter)
        {
            OrderService.Service data_access = new OrderService.Service();
            OrderService.AuthHeader _orderHeader = new OrderService.AuthHeader();
            _orderHeader.Password = "nimda";
            _orderHeader.Username = "admin";
            data_access.AuthHeaderValue = _orderHeader;
            return data_access.GetDataSQLExec(sql_procedure, sql_parameter);
        }

        public static string GetStringFromProcedure(string sql_procedure, string sql_parameter)
        {
            OrderService.Service data_access = new OrderService.Service();
            OrderService.AuthHeader _orderHeader = new OrderService.AuthHeader();
            _orderHeader.Password = "nimda";
            _orderHeader.Username = "admin";
            data_access.AuthHeaderValue = _orderHeader;
            return data_access.CheckDataSQLExec(sql_procedure, sql_parameter);
        }

        public static string ExecDataFromProcedure(string sql_procedure, string sql_parameter)
        {
            OrderService.Service data_access = new OrderService.Service();
            OrderService.AuthHeader _orderHeader = new OrderService.AuthHeader();
            _orderHeader.Password = "nimda";
            _orderHeader.Username = "admin";
            data_access.AuthHeaderValue = _orderHeader;
            return data_access.UpdateDataSQLExec(sql_procedure, sql_parameter);
        }
        
        //End code voi TuLM ExecDataFromProcedure
        //public static void ExportToExcel(Xceed.Grid.GridControl gridControl1)
        //{
        //    System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
        //    saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        //    saveFileDialog.Filter = "Excel Files (*.xls)|*.xls";
        //    saveFileDialog.AddExtension = true;
        //    if (saveFileDialog.ShowDialog(gridControl1.Parent) == System.Windows.Forms.DialogResult.OK)
        //    {
        //        string FileName = saveFileDialog.FileName;
        //        // TODO: Add code here to save the current contents of the form to a file.
        //        Xceed.Grid.Exporting.ExcelExporter export = new Xceed.Grid.Exporting.ExcelExporter();
        //        export.Export(gridControl1, FileName);
        //    }
        //}

        public static void FormatC1Grid(ref C1.Win.C1TrueDBGrid.C1TrueDBGrid data_grid)
        {
            foreach (C1.Win.C1TrueDBGrid.C1DataColumn col in data_grid.Columns)
            {
                
                switch (col.DataType.ToString())
                {
                    case "System.Decimal":
                        col.NumberFormat = global::IceAge.Properties.Settings.Default.CurrencyFormat;                          
                        break;
                    case "System.DateTime":
                        col.NumberFormat = global::IceAge.Properties.Settings.Default.DateDisplayFormat;
                        break;
                    default:
                        break;
                }
            }
        }
        public static void AggregateC1Grid(ref C1.Win.C1TrueDBGrid.C1TrueDBGrid data_grid)
        {
            DataTable data_src = (DataTable) data_grid.DataSource;
            decimal total_value = 0;
            if (data_src != null){
                foreach (C1.Win.C1TrueDBGrid.C1DataColumn col in data_grid.Columns)
                {
                    total_value = 0;
                    switch (col.Aggregate)
                    {
                        case C1.Win.C1TrueDBGrid.AggregateEnum.Average:
                            break;
                        case C1.Win.C1TrueDBGrid.AggregateEnum.Count:
                            if (data_src.Compute("count(" + col.DataField + ")", string.Empty) != DBNull.Value)
                                total_value = Convert.ToDecimal(data_src.Compute("count(" + col.DataField + ")", string.Empty));
                            col.FooterText = total_value.ToString(global::IceAge.Properties.Settings.Default.CurrencyFormat);
                            break;
                        case C1.Win.C1TrueDBGrid.AggregateEnum.Custom:
                            break;
                        case C1.Win.C1TrueDBGrid.AggregateEnum.Max:
                            break;
                        case C1.Win.C1TrueDBGrid.AggregateEnum.Min:
                            break;
                        case C1.Win.C1TrueDBGrid.AggregateEnum.None:
                            break;
                        case C1.Win.C1TrueDBGrid.AggregateEnum.Std:
                            break;
                        case C1.Win.C1TrueDBGrid.AggregateEnum.StdPop:
                            break;
                        case C1.Win.C1TrueDBGrid.AggregateEnum.Sum:
                            if (data_src.Compute("sum(" + col.DataField + ")", string.Empty) != DBNull.Value)
                                total_value = Convert.ToDecimal(data_src.Compute("sum(" + col.DataField + ")", string.Empty));
                            col.FooterText = total_value.ToString(global::IceAge.Properties.Settings.Default.CurrencyFormat);
                            break;
                        case C1.Win.C1TrueDBGrid.AggregateEnum.Var:
                            break;
                        case C1.Win.C1TrueDBGrid.AggregateEnum.VarPop:
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        public static void ExportC1Grid(ref C1.Win.C1TrueDBGrid.C1TrueDBGrid grdResult)
        {
            System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "Excel Files (*.xls)|*.xls";
            saveFileDialog.AddExtension = true;
            if (saveFileDialog.ShowDialog(grdResult.Parent) == System.Windows.Forms.DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
                // TODO: Add code here to save the current contents of the form to a file.
                //data_grid.ExportToExcel(FileName);  
                System.IO.StreamWriter fs = new System.IO.StreamWriter(FileName, false);
                fs.WriteLine("<?xml version=\"1.0\"?>");
                fs.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>");
                fs.WriteLine("<ss:Workbook xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">");
                fs.WriteLine(" <ss:Styles>");
                fs.WriteLine(" <ss:Style ss:ID=\"1\">");
                fs.WriteLine(" <ss:Font ss:Bold=\"1\"/>");
                fs.WriteLine(" </ss:Style>");
                fs.WriteLine(" </ss:Styles>");
                fs.WriteLine(" <ss:Worksheet ss:Name=\"Sheet1\">");
                fs.WriteLine(" <ss:Table>");
                int iCol;
                for (iCol = 0; iCol <= grdResult.Columns.Count - 1; iCol++)
                {
                    if (grdResult.Splits[0].DisplayColumns[iCol].Visible == true)
                    {
                        fs.WriteLine(" <ss:Column ss:Width=\"{0}\"/>", grdResult.Splits[0].DisplayColumns[iCol].Width);
                    }
                }

                fs.WriteLine(" <ss:Row ss:StyleID=\"1\">");

                for (iCol = 0; iCol <= grdResult.Splits[0].DisplayColumns.Count - 1; iCol++)
                {
                    if (grdResult.Splits[0].DisplayColumns[iCol].Visible == true)
                    {
                        fs.WriteLine(" <ss:Cell>");
                        fs.WriteLine(String.Format(" <ss:Data ss:Type=\"String\">{0}</ss:Data>", grdResult.Splits[0].DisplayColumns[iCol].DataColumn.Caption));
                        fs.WriteLine(" </ss:Cell>");
                    }
                }

                fs.WriteLine(" </ss:Row>");
                int iRow;

                for (iRow = 0; iRow <= grdResult.RowCount - 1; iRow++)
                {
                    fs.WriteLine(String.Format(" <ss:Row ss:Height =\"{0}\">", grdResult.RowHeight));
                    for (iCol = 0; iCol <= grdResult.Splits[0].DisplayColumns.Count - 1; iCol++)
                    {
                        if (grdResult.Splits[0].DisplayColumns[iCol].Visible == true)
                        {
                            fs.WriteLine(" <ss:Cell>");
                            fs.WriteLine(String.Format(" <ss:Data ss:Type=\"String\">{0}</ss:Data>", grdResult.Splits[0].DisplayColumns[iCol].DataColumn.CellValue(iRow)));
                            //fs.WriteLine(String.Format(" <ss:Data ss:Type=\"String\">{0}</ss:Data>", grdResult.Columns[iCol].CellText(iRow)));
                            //switch (grdResult.Columns[iCol].DataType.ToString())
                            //{
                            //    case "System.String":
                            //        fs.WriteLine(String.Format(" <ss:Data ss:Type=\"String\">{0}</ss:Data>", grdResult.Splits[0].DisplayColumns[iCol].DataColumn.CellValue(iRow)));
                            //        break;                            
                            //    case "System.DateTime":
                            //        fs.WriteLine(String.Format(" <ss:Data ss:Type=\"DateTime\">{0}</ss:Data>", grdResult.Splits[0].DisplayColumns[iCol].DataColumn.CellValue(iRow)));
                            //        break;
                            //    default:
                            //        fs.WriteLine(String.Format(" <ss:Data ss:Type=\"String\">{0}</ss:Data>", grdResult.Splits[0].DisplayColumns[iCol].DataColumn.CellValue(iRow)));
                            //        break;
                            //}
                            fs.WriteLine(" </ss:Cell>");
                        }
                    }
                    fs.WriteLine(" </ss:Row>");
                }

                fs.WriteLine(" </ss:Table>");
                fs.WriteLine(" </ss:Worksheet>");
                fs.WriteLine("</ss:Workbook>");

                fs.Close();
            }
        }
        public static string MD5CryptPassword(string password){
            System.Security.Cryptography.MD5CryptoServiceProvider md5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] hashedBytes;
            System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
            hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(password));
            return System.BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
        }
        #endregion
        
    }
}
